package com.hopeland.androidpc.jilintongdaomen;

import static com.hopeland.androidpc.jilintongdaomen.MainDataActivity.defaut_address;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hopeland.androidpc.jilintongdaomen.network.AddDataBean;
import com.hopeland.androidpc.jilintongdaomen.network.KillerApi;
import com.hopeland.androidpc.jilintongdaomen.network.MainApi;
import com.hopeland.androidpc.jilintongdaomen.network.MyObserve;
import com.hopeland.androidpc.jilintongdaomen.network.QueryRfidBean;
import com.hopeland.androidpc.jilintongdaomen.room.RfidDataBase;
import com.hopeland.androidpc.jilintongdaomen.room.entry.RfidRoomInfo;
import com.rfidread.RFIDReader;
import com.util.AddRfidsBean;
import com.util.AddressEvent;
import com.util.CommonItemView;
import com.util.CommonPopupList;
import com.util.LogUtils;
import com.util.PreferenceUtils;
import com.util.RecyclerViewUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.MaybeObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SettingActivity extends Activity {



    TextView tv_power,btn_url,btn_address;   //功率
    EditText ed_url,ed_adress;

    FrameLayout drop_power;

    CommonItemView item_power;

    private CommonPopupList mPowerPopup;
    protected static int _Min_Power = 0; // 读写器最小发射功率

    private String[] powers_G = { "0", "1","2", "3", "4", "5","6", "7", "8", "9","10", "11", "12", "13","14", "15", "16", "17","18", "19", "20", "21","22", "23", "24", "25", "26","27", "28", "29", "30", "31", "32", "33" };

    String devices_address;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 创建
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.item_setting);

        tv_power = findViewById(R.id.tv_power);
        drop_power = findViewById(R.id.drop_power);
        item_power = findViewById(R.id.item_power);
        btn_url = findViewById(R.id.btn_url);
        btn_address = findViewById(R.id.btn_address);
        ed_url = findViewById(R.id.ed_url);
        ed_adress = findViewById(R.id.ed_adress);

        mPowerPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(drop_power)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tv_power.setText(item);
                        button_SetPower(Integer.parseInt(item));
                    }
                })
                .build();


        mPowerPopup.setData(powers_G);



        String http_url = PreferenceUtils.getData(App.application,PreferenceUtils.http_url);
        if (TextUtils.isEmpty(http_url)){
            PreferenceUtils.setData(App.application,PreferenceUtils.http_url,MainDataActivity.defaut_url);
        }
        ed_url.setText(http_url);
         devices_address = PreferenceUtils.getData(App.application,PreferenceUtils.device_address);
        if (TextUtils.isEmpty(devices_address)){
            PreferenceUtils.setData(App.application,PreferenceUtils.device_address,defaut_address);
        }
        ed_adress.setText(devices_address);
        btn_url.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = ed_url.getText().toString();
                PreferenceUtils.setData(App.application,PreferenceUtils.http_url,str);
                MainDataActivity.http_url = str;
            }
        });
        btn_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = ed_adress.getText().toString();
                PreferenceUtils.setData(App.application,PreferenceUtils.device_address,str);
                MainDataActivity.devices_address = str;
                EventBus.getDefault().post(new AddressEvent());
                finish();
            }
        });

        drop_power.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPowerPopup.showAsDropDown(v);
            }
        });

        String sPower  = RFIDReader._Config.GetANTPowerParam2(devices_address);
        //tv_power.setText(sPower);
        String delimiter = "&2";
        String result = substringBefore(sPower, delimiter);
        result = result.substring(2);
        tv_power.setText(result);
        LogUtils.e("sPower="+sPower);
    }

    public static String substringBefore(String str, String delimiter) {
        if (str == null || delimiter == null || str.isEmpty()) {
            return ""; // 处理空字符串和空字符的情况
        }

        int index = str.indexOf(delimiter);
        if (index == -1) {
            return str; // 如果没有找到字符，返回原字符串
        }

        return str.substring(0, index); // 返回指定字符前的数据
    }

    // 设置功率
    public void button_SetPower(int power) {

        try {
            HashMap<Integer, Integer> dicPower = new HashMap<>();

            dicPower.put(1, _Min_Power + power);
            dicPower.put(2, _Min_Power + power);
            dicPower.put(3, _Min_Power + power);
            dicPower.put(4, _Min_Power + power);
//            dicPower.put(5, _Min_Power + power);
//            dicPower.put(6, _Min_Power + power);
//            dicPower.put(7, _Min_Power + power);
//            dicPower.put(8, _Min_Power + power);


            int rt = RFIDReader._Config.SetANTPowerParam(devices_address, dicPower);
            if (rt == 0) {
                Toast.makeText(SettingActivity.this,"设置成功",Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(SettingActivity.this,"设置失败",Toast.LENGTH_SHORT).show();
            }
        }catch (Exception ex){}
    }

}
